package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.RedAffair;
import com.ruoyi.common.core.domain.entity.Region;
import com.ruoyi.system.domain.WhiteAffair;
import com.ruoyi.system.service.IRedAffairService;
import com.ruoyi.system.service.IWhiteAffairService;
import com.ruoyi.system.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.ruoyi.common.utils.SecurityUtils.getDeptId;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {
    @Autowired
    private IRedAffairService redAffairService;
    @Autowired
    private IWhiteAffairService whiteAffairService;
    @Autowired
    private RegionService regionService;

    /**
     * 信息申报数量
     *
     * @return
     */
    @GetMapping("/affairNum")
    public AjaxResult affairNum() {
        Map<String, Object> map = new HashMap<>();
        List<RedAffair> redlist = redAffairService.selectRedAffairList(new RedAffair(), null, null);
        List<WhiteAffair> whitelist = whiteAffairService.selectWhiteAffairList(new WhiteAffair(), null, null);
        map.put("Allred", redlist.size());
        map.put("Allwhite", whitelist.size());

        int monthred = 0, monthwhite = 0, dayred = 0, daywhite = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date first = calendar.getTime();//本月第一个天
        calendar.roll(Calendar.DAY_OF_MONTH, -1);
        Date last = calendar.getTime();//本月最后一天
        for (RedAffair affair : redlist) {
            Date date = new Date();
            String d1 = affair.getAffairTime().split("至")[0];
            String d2 = affair.getAffairTime().split("至")[1];
            Date date1 = DateUtils.StrToDate(d1);
            Date date2 = DateUtils.StrToDate(d2);
            if (date1.compareTo(date) <= 0 && date2.compareTo(date) >= 0) {
                dayred += 1;
            }
            if ((first.getTime() <= date1.getTime() && last.getTime() >= date1.getTime()) || (first.getTime() <= date2.getTime() && last.getTime() >= date2.getTime())) {
                monthred += 1;
            }

        }
        for (WhiteAffair affair : whitelist) {
            Date date = new Date();
            String d1 = affair.getAffairTime().split("至")[0];
            String d2 = affair.getAffairTime().split("至")[1];
            Date date1 = DateUtils.StrToDate(d1);
            Date date2 = DateUtils.StrToDate(d2);
            if (date1.compareTo(date) <= 0 && date2.compareTo(date) >= 0) {
                daywhite += 1;
            }
            if ((first.getTime() <= date1.getTime() && last.getTime() >= date1.getTime()) || (first.getTime() <= date2.getTime() && last.getTime() >= date2.getTime())) {
                monthwhite += 1;
            }
        }
        map.put("monthred", monthred);
        map.put("dayred", dayred);
        map.put("monthwhite", monthwhite);
        map.put("daywhite", daywhite);
        return AjaxResult.success(map);
    }

    /**
     * @param id    地区ID
     * @param begin 开始时间
     * @param end   结束时间
     * @return
     */
    @GetMapping("/villageNum")
    public AjaxResult villageNum(String id, String begin, String end) {
        String username = getUsername();
        if (!"admin".equals(username) && !"雄安新区".equals(username)) {
            id = getDeptId();
        }
        RedAffair redAffair = new RedAffair();
        Region r = regionService.getId(id);
//        redAffair.setVillage(r.getName());
        WhiteAffair whiteAffair = new WhiteAffair();
//        whiteAffair.setVillage(r.getName());
        List<RedAffair> redlist = redAffairService.selectRedAffairList(redAffair, null, null);
        List<WhiteAffair> whitelist = whiteAffairService.selectWhiteAffairList(whiteAffair, null, null);
        List<RedAffair> list1 = new ArrayList<>();
        List<WhiteAffair> list2 = new ArrayList<>();
        if (StringUtils.isNotEmpty(begin) && StringUtils.isNotEmpty(end)) {
            Date begin1 = new Date(Long.parseLong(String.valueOf(begin)));
            Date end1 = new Date(Long.parseLong(String.valueOf(end)));
            for (RedAffair affair : redlist) {
                if ((begin1.getTime() <= affair.getBeginDate().getTime() && end1.getTime() >= affair.getBeginDate().getTime()) || (begin1.getTime() <= affair.getEndDate().getTime() && end1.getTime() >= affair.getEndDate().getTime())) {
                    list1.add(affair);
                }
            }
            for (WhiteAffair affair : whitelist) {
                if ((begin1.getTime() <= affair.getBeginDate().getTime() && end1.getTime() >= affair.getBeginDate().getTime()) || (begin1.getTime() <= affair.getEndDate().getTime() && end1.getTime() >= affair.getEndDate().getTime())) {
                    list2.add(affair);
                }
            }
        } else {
            list1.addAll(redlist);
            list2.addAll(whitelist);
        }
        List<Region> regions = regionService.Regionlist(id);
        if (regions.size() == 0) {  //最后一级查询自己
            regions = regionService.Regionlistzj(id);
        }
        List<Map<String, Object>> list = new ArrayList<>();
        for (Region region : regions) {
            Map<String, Object> m1 = new HashMap<>();
            m1.put("name", region.getName());
            int i = 0, y = 0;
            for (RedAffair affair : list1) {
                if (affair.getVillage().indexOf(region.getName()) > -1) {
                    i += 1;
                }
            }
            m1.put("red", i);
            for (WhiteAffair affair : list2) {
                if (affair.getVillage().indexOf(region.getName()) > -1) {
                    y += 1;
                }
            }
            m1.put("white", y);
            list.add(m1);
        }
        return AjaxResult.success(list);
    }

    @GetMapping(value = "/item/{id}")
    public AjaxResult getItem(@PathVariable String id) {
        String username = getUsername();
        if (!"admin".equals(username)) {
            id = getDeptId();
        }
        List<Region> regionlist = regionService.Regionlist(id);
        if (regionlist.size() == 0) {  //最后一级查询自己
            regionlist = regionService.Regionlistzj(id);
        }
        return AjaxResult.success(regionlist);
    }
}
